package com.limm.dao.register;

import com.limm.util.DbUtil;
import com.limm.util.Util;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class RegisterDao {
    private Connection conn = DbUtil.getConn();
    private PreparedStatement state = null;
    //将用户资料插入进数据库
    public void insert(String id,String username,String email, String password,String data) {

        try {
            String sql = "INSERT INTO users(u_id,u_name,u_email, u_password,u_creattime)VALUES (?,?,?,?,?)";
            state = conn.prepareStatement(sql);
            state.setString(1, id);
            state.setString(2, username);
            state.setString(3, email);
            state.setString(4, password);
            state.setString(5, data);
            state.executeUpdate();
        } catch (Exception e) {
            System.out.println("插入失败");
        } finally {
            try {
                state.close();
            } catch (Exception e) {

            }

        }

    }

    //判断邮箱是否存在  true--存在   false--不存在
    public boolean isExistOfEmail(String email) {
        try {
            String sql = "SELECT * FROM users WHERE u_email = ?";
            PreparedStatement state = conn.prepareStatement(sql);
            state.setString(1, email);
            ResultSet result = state.executeQuery();
            if (result.next()) {
                if (result.getString("u_email").equals(email)) {
                    return true;//存在
                } else {
                    return false;//不存在
                }
            }
        } catch (Exception e) {
            System.out.println("注册查找失败");
        }
        return false;
    }

}
